package com.zj.leetcode.medium.greed;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 最大数
 * 179 largest-number
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class LargestNumber {
    public static void main(String[] args) {
        System.out.println("82".compareTo("332"));
        Solution solution = new LargestNumber().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * 通过compareTo方法，字典序的实现，来解这道题
         * @param nums
         * @return
         */
        public String largestNumber(int[] nums) {

            if (nums == null || nums.length == 0) {
                return "";
            }

            String[] strArr = new String[nums.length];
            for (int i = 0; i < strArr.length; i++) {
                strArr[i] = String.valueOf(nums[i]);
            }

            Arrays.sort(strArr, (o1, o2) -> (o2 + o1).compareTo(o1 + o2));

            StringBuilder sb = new StringBuilder();
            for (String aStrArr : strArr) {
                sb.append(aStrArr);
            }
            String result = sb.toString();

            if (result.charAt(0) == '0') {
                result = "0";
            }
            return result;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
